package com.lzx.adx.report.support.strategy.v3.media;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.google.common.collect.Lists;
import com.lzs.adx.commmon.utils.JsonUtils;
import com.lzx.adx.report.annnotions.MediaType;
import com.lzx.adx.report.constant.MediaChannelConstant;
import com.lzx.adx.report.dto.CallTypeMappingDTO;
import com.lzx.adx.report.dto.GtdResult;
import com.lzx.adx.report.dto.OcpxAccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.tencent.request.GTdCallBackRequest;
import com.lzx.adx.report.model.tencent.request.callback.Params;
import com.lzx.adx.report.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 广点通小程序回调
 */
@Component
@Slf4j
@MediaType(MediaChannelConstant.GDT_APP)
public class GdtAppChannel extends AbstractChannel {
    @Autowired
    private ReportService reportService;
    @Override
    public ResponseEntity callback(ReportDTO reportDTO, OcpxAccountDTO accountDTO, String callType, String ts) {
        boolean base = base(reportDTO, accountDTO, callType, ts);
        if (!base) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        //如果点击id有那么久坐精准匹配 如果没有则模糊匹配
        if (StringUtils.isNotBlank(reportDTO.getRequestId())) {
            ReportDTO reportData = reportService.getReportData(reportDTO.getRequestId(), reportDTO.getDspId());
            if (null == reportData) {
                log.info("账号为{} 请求流水号{} 点击id:[{}]精准匹配失败", reportDTO.getDspId(), reportDTO.getBizRequestId(), reportDTO.getRequestId());
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            reportDTO = reportData;
            log.info("账号为{} 请求流水号{} 点击id:[{}]精准匹配成功", reportDTO.getDspId(), reportDTO.getBizRequestId(), reportDTO.getRequestId());
        }
        Map<String, CallTypeMappingDTO> callTypeMap = getCallTypeMap(accountDTO);
        CallTypeMappingDTO callTypeMappingDTO = callTypeMap.get(callType);
        String eventType = callTypeMappingDTO.getMediaReturnType();
        Params params1 = new Params();
        Long actionTime=Long.valueOf(ts) / 1000;
        params1.setAction_time(actionTime);
        params1.setAction_type(eventType);
        params1.setOuter_action_id(reportDTO.getBizRequestId());
        params1.setAction_param(new HashMap<>());
        GTdCallBackRequest gTdCallBackRequest = new GTdCallBackRequest();
        gTdCallBackRequest.setActions(Lists.newArrayList(params1));
        String paramsBody = JsonUtils.toJsonString(gTdCallBackRequest);
        log.info("账号为{} 请求流水号{} 广点通小程序回调的请求的参数为{}", reportDTO.getDspId(), reportDTO.getBizRequestId(), paramsBody);
        String url = URLUtil.decode(reportDTO.getCallback());
        HttpRequest body = HttpUtil.createPost(url).body(paramsBody);
        String url1 = body.getUrl();
        System.out.println(url1);
        String result = body.execute().body();
        log.info("账号为{} 请求流水号{} 上报给广点通小程序转换响应的结果为{}", reportDTO.getDspId(), reportDTO.getBizRequestId(), result);
        accountDTO.setFinalURL(url);
        accountDTO.setBody(result);
        return ResponseEntity.ok(new GtdResult<>());
    }

    /**
     * 测试回传代码,从数据库拿到回调地址 替换运行即可
     * @param args
     */
    public static void main(String[] args) {
        Params params1 = new Params();
        params1.setAction_time(Long.valueOf(System.currentTimeMillis()) / 1000);
        params1.setAction_type("COMPLETE_ORDER");
        params1.setOuter_action_id("438dbd57e289456bb785a61ae26a70a7zhongmian08");
        params1.setAction_param(new HashMap<>());
        GTdCallBackRequest gTdCallBackRequest = new GTdCallBackRequest();
        gTdCallBackRequest.setActions(Lists.newArrayList(params1));
        String paramsBody = JsonUtils.toJsonString(gTdCallBackRequest);
        String url = URLUtil.decode("http://tracking.e.qq.com/conv?cb=acc_H5TY9l-Y7fkuSX5iWD_sCgl-gDvnDV-g0v-tDLMZXIZ6L14r4pOBWE9yjGYv&conv_id=51521764");
        HttpRequest body = HttpUtil.createPost(url).body(paramsBody);

        String result = body.execute().body();
        System.out.println(result);
    }
}
